package com.hengyu.data.validator.factory.validators;

import com.hengyu.data.validator.exception.ValidatorException;

import java.lang.reflect.Field;

/**
 * 验证@Null注解逻辑
 * ========================
 * Created with IntelliJ IDEA.
 * User：恒宇少年
 * Date：2017/3/19
 * Time：16:39
 * 码云：http://git.oschina.net/jnyqy
 * ========================
 */
public class NullValidator implements Validator
{
    /**
     * 验证不通过时直接抛出ValidatorException异常
     * @param field 注解字段
     * @throws ValidatorException
     */
    public boolean validate(Field field,Object object) throws Exception {
        if(field == null)
        {
            throw new ValidatorException("未找到验证反射对象");
        }
        field.setAccessible(true);
        //获取字段对象
        Object fieldValue = field.get(object);
        //返回验证
        return fieldValue == null;
    }
}
